﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using grupo2DEWStudio7i.Negocio;
using grupo2DEWStudio7i.Dominio;
using grupo2DEWStudio7i.Persistencia;

namespace grupo2DEWStudio7i.Controllers
{
    public class ClienteController : Controller
    {

        //Dependencias
        public AdministracionServiceImpl AdminService { get; set; }
        // GET: /Cliente/

        public ActionResult Index()
        {
            ICollection<Cliente> lista = AdminService.ListarCliente();

            return View(lista);
        }

        //
        // GET: /Cliente/Details/5

        public ActionResult Details(int cod)
        {
            Cliente modelo = AdminService.ObtenerCliente(cod);
            return View(modelo);
        }

        //
        // GET: /Cliente/Create
        public ActionResult Create()
        {
            try
            {
                return View();
            }
            catch
            {
                ModelState.AddModelError(String.Empty, "Error al crear Cliente");
                return View();
            }
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            Cliente clienteaCrear = new Cliente();
            {

                clienteaCrear.Nombre = (string)collection["Nombre"];
                clienteaCrear.Dni = (string)collection["Dni"];
                clienteaCrear.Clave = (string)collection["Clave"];
                clienteaCrear.Correo = (string)collection["Correo"];
                clienteaCrear.FecNac = (string)collection["FecNac"];
            };
            try
            {
                Cliente clienteCreado = AdminService.RegistrarCliente(clienteaCrear.Nombre, clienteaCrear.Dni, clienteaCrear.Clave, clienteaCrear.Correo, clienteaCrear.FecNac);//clienteaCrear);
                TempData["mensaje"] = "Cliente " + clienteCreado.Codigo + " creado exitosamente";
                //}
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, "Error al crear Cliente: " + ex.Message);
                return View("Create", clienteaCrear);
            }
        }

        //
        // GET: /Cliente/Edit/5

        public ActionResult Edit(int id)
        {
            Cliente clienteaCrear = AdminService.ObtenerCliente(id);

            return View(clienteaCrear);
        }

        //
        // POST: /Cliente/Edit/5

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            Cliente clienteaModificar = new Cliente();
            {
                clienteaModificar.Codigo = int.Parse(collection["Codigo"]);
                clienteaModificar.Nombre = (string)collection["Nombre"];
                clienteaModificar.Dni = (string)collection["Dni"];
                clienteaModificar.Clave = (string)collection["Clave"];
                clienteaModificar.Correo = (string)collection["Correo"];
                clienteaModificar.FecNac = (string)collection["FecNac"];

            };
            try
            {

                Cliente clienteModificado = AdminService.ModificarCliente(clienteaModificar);
                TempData["mensaje"] = "Cliente " + clienteModificado.Codigo + " modificado exitosamente";
                //}
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, "Error al crear Cliente: " + ex.Message);
                return View("Edit", clienteaModificar);
            }
        }

        //
        // GET: /Cliente/Delete/5

        public ActionResult Delete(int id)
        {
            return View(AdminService.ObtenerCliente(id));
        }

        //
        // POST: /Cliente/Delete/5

        [HttpPost]
        public ActionResult Eliminar(int id)
        {
            try
            {
                Cliente clienteaEliminar = AdminService.ObtenerCliente(id);
                AdminService.EliminarCliente(clienteaEliminar);
                TempData["mensaje"] = "Cliente " + clienteaEliminar.Codigo + " eliminado exitosamente";
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {

                ModelState.AddModelError(string.Empty, ex.Message);
                return View("Delete", AdminService.ObtenerCliente(id));
            }
        }


    }
}
